import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from pylab import mpl

mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False

url = 'https://population.gotohui.com/'
response = requests.get(url)
response.encoding = 'utf-8'  # 指定编码为 utf-8
soup = BeautifulSoup(response.text, "html.parser")
soup = BeautifulSoup(response.text, 'html.parser')

# 检查表格类名或ID
table = soup.find('table')
if table is None:
    print("No table found")
else:
    # 提取数据
    population_data = []
    rows = table.find_all('tr')[1:11]  # 只爬取前10条信息
    for row in rows:
        cols = row.find_all('td')
        if cols:
            time = cols[0].text.strip()
            population = cols[1].text.strip()
            birth_rate = cols[2].text.strip()
            growth_rate = cols[3].text.strip()
            old_age = cols[4].text.strip()
            child = cols[5].text.strip()
            man = cols[6].text.strip()
            woman = cols[7].text.strip()
            National_Birth_Population = cols[8].text.strip()
            National_death_Population = cols[9].text.strip()
            population_data.append(
                [time, population, birth_rate, growth_rate, old_age, child, man, woman, National_Birth_Population,
                 National_death_Population])

    # 将数据整理成 DataFrame
    df = pd.DataFrame(population_data,
                      columns=['时间', '人口(万人)', '出生率(‰)', '增长率(‰)', '老年(‰)', '儿童(‰)', '男性(‰)',
                               '女性(‰)', '全国出生人口(万人)', '全国死亡人口(万人)'])

    # 将数据写入 Excel 文件
    excel_file = '小组21.xlsx'
    df.to_excel(excel_file, index=False)

    # 读取 Excel 文件并生成词云
    df = pd.read_excel(excel_file)

    # 可视化分析
    # 柱状图
    plt.figure(figsize=(10, 6))
    plt.bar(df['时间'], df['人口(万人)'], color='skyblue')
    plt.xlabel('时间')
    plt.ylabel('人口(万人)')
    plt.title('近十年中国人口')
    plt.xticks(rotation=90)
    plt.tight_layout()
    plt.savefig('小组21_bar_chart.png')
    plt.show()

    # 折线图
    plt.figure(figsize=(10, 6))
    plt.plot(df['时间'], df['出生率(‰)'], marker='o', color='orange', label='出生率(‰)')
    plt.plot(df['时间'], df['增长率(‰)'], marker='x', color='green', label='增长率(‰)')
    plt.xlabel('时间')
    plt.ylabel('中国人口出生及增长率(‰)')
    plt.title('近十年中国人口')
    plt.xticks(rotation=90)
    plt.legend()
    plt.tight_layout()
    plt.savefig('小组21_line_chart.png')
    plt.show()

    # 生成性别分布的饼图
    gender_labels = ['男性', '女性']
    gender_counts = [df['男性(‰)'].astype(float).sum(), df['女性(‰)'].astype(float).sum()]
    plt.figure(figsize=(8, 8))
    plt.pie(gender_counts, labels=gender_labels, autopct='%1.1f%%', startangle=140)
    plt.title('性别分布')
    plt.savefig('小组21_pie_chart.png')
    plt.show()
